<?php

/* ========================================================================= */
/* This file is part of the HypnoPHP Framework Library                       */
/* Copyright (c) 2009-2010, Radu Buzea                                       */
/* ========================================================================= */

/**
* @author Radu Buzea
* @copyright Copyright (c) 2009-2010, Radu Buzea (www.hypnosis.ro)
* @license GNU Lesser General Public License (www.gnu.org/copyleft/lesser.html)
* @package HypnoPHP
* @category Core
* @version 1.0.3
*/

defined('HYPNOPHP_APPLICATION') or
die("Cannot use <i>" . basename(__FILE__) . "</i> outside HypnoPHP Application");

// ============================================================================
// Final class: HCookie
// ============================================================================

final class HCookie extends HBaseObject
{

	/**
	* Initialize the application cookies
	*
	* @return void
	*/
	public static function initCookie()
	{
		$cookies = self::getInstance();
		$cookies->cookie_params = array();
		self::start();
	}

	# ###########################################################################

	/**
	* Start the application cookies
	*
	* @return void
	*/
	public static function start()
	{
		$cookie_params = array();
		if (count($_COOKIE) > 0) {
			foreach ($_COOKIE as $param => $value) {
				$cookie_params[$param] = array(
					'name' => $param,
					'value' => $value,
					'expires' => 0,
					'domain' => '',
					'secure' => false,
				);
			}
		}
		$cookies = self::getInstance();
		$cookies->cookie_params = $cookie_params;
	}

	# ###########################################################################

	/**
	* Check if a variable is registered in the cookies list
	*
	* @param string $param The variable name
	* @return boolean
	*/
	public static function registered($param = '')
	{
		$param = trim($param);
		if ($param == '') { return false; }
		$cookies = self::getInstance();
		$cookie_params = $cookies->cookie_params;
		return isset($cookie_params[$param]) && !empty($cookie_params[$param]);
	}

	# ###########################################################################

	/**
	* Get the internal data structure of a cookie variable
	*
	* @param string $name The cookie name
	* @return array or null if the cookie doesn't exists
	*/
	public static function getCookie($name = '')
	{
		$cookies = self::getInstance();
		$cookie_params = $cookies->cookie_params;
		if (trim($name) == '' || !isset($cookie_params[$name])) { return null; }
		return $cookie_params[$name];
	}

	# ###########################################################################

	/**
	* Get the value of a cookie variable
	*
	* @param string $name The cookie name
	* @return mixed or null if the cookie doesn't exists
	*/
	public static function getVar($name = '')
	{
		$cookie = self::getCookie($name);
		if (!is_array($cookie) || !isset($cookie['value'])) { return null; }
		return $cookie['value'];
	}

	# ###########################################################################

	/**
	* Save a cookie variable
	*
	* @param string $name The cookie variable name
	* @param mixed $value The cookie value
	* @param mixed $expires The time the cookie expires
	* @param mixed $path The path on the server in which the cookie will be available on
	* @param mixed $domain The domain that the cookie is available
	* @return bool
	*/
	public static function setCookie($name = '', $value = null,
		$expires = 0, $path = '/', $domain = '')
	{
		$name = trim($name);
		if ($name == '') { return false; }
		$cookies = self::getInstance();
		$cookie_params = $cookies->cookie_params;
		if ($value == null && isset($cookie_params[$name])) {
			unset($cookie_params[$name]);
		} else {
			$cookie_params[$name] = array(
				'name' => $name,
				'value' => $value,
				'expires' => $expires,
				'domain' => $domain,
				'secure' => HRequest::isSSL(),
			);
		}
		$cookies->cookie_params = $cookie_params;
		return true;
	}

	# ###########################################################################

	/**
	* Clear all variables from the cookies list
	*
	* @param mixed $global_session (optional) Clear variable form the $_COOKIE globals only
	* @return void
	*/
	public static function clear($global_cookie = false)
	{
		if ($global_cookie) {
			foreach ($_COOKIE as $key => $value) {
				setcookie($key, null, time() - 86400);
			}
			$_COOKIE = array();
		} else {
			$cookies = self::getInstance();
			$cookies->cookie_params = array();
		}
	}

	# ###########################################################################

	/**
	* Send all the cookies
	*
	* @return void
	*/
	public static function saveCookies()
	{
		self::clear(true);
		$cookies = self::getInstance();
		$cookie_params = $cookies->cookie_params;
		if (!is_array($cookie_params) || count($cookie_params) == 0) { return; }
		$session_name_cookie = session_name();
		foreach ($cookie_params as $param => $value) {
			if ($param == $session_name_cookie) { continue; }
			if (isset($value['name']) && isset($value['value'])) {
				$name = $value['name'];
				$value = $value['value'];
				if (isset($value['expires'])) { $expires = intval($value['expires']); }
				else { $expires = 0; }
				if (isset($value['path'])) { $path = $value['path']; }
				else { $path = '/'; }
				if (isset($value['domain'])) { $domain = $value['domain']; }
				else { $domain = ''; }
				if (isset($value['secure'])) { $secure = $value['secure']; }
				else { $secure = HRequest::isSSL(); }
				setcookie($name, $value, $expires, $path, $domain, $secure);
			}
		}
	}

	# ###########################################################################

	/**
	* Get the application cookies instance (singleton)
	*
	* @return HCookie
	*/
	public static function getInstance()
	{
		return self::singleton(__CLASS__);
	}

}
